package top.zywork.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.zywork.common.AuthUtils;
import top.zywork.common.WebUtils;
import top.zywork.enums.AppTypeEnum;
import top.zywork.enums.ClientAppEnum;
import top.zywork.enums.SysConfigEnum;
import top.zywork.service.ContentSecCheckService;
import top.zywork.service.SysConfigService;
import top.zywork.weixin.WeixinSecCheckUtils;
import top.zywork.weixin.WeixinXcxConfig;

import java.io.InputStream;

/**
 * 内容检测服务接口实现类<br/>
 *
 * 创建于2020-02-26<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@Service(value = "contentSecCheckService")
public class ContentSecCheckServiceImpl implements ContentSecCheckService {

    private SysConfigService sysConfigService;

    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean checkText(String text) {
        String appid = WebUtils.getServletRequest().getHeader(AuthUtils.HEADER_PREFIX + AuthUtils.PARAM_APPID);
        if (StringUtils.isNotEmpty(appid) && appid.startsWith(AppTypeEnum.WEIXIN_XCX.getValue())) {
            WeixinXcxConfig weixinXcxConfig = sysConfigService.getByName(SysConfigEnum.WEIXIN_XCX_CONFIG.getValue(), WeixinXcxConfig.class);
            return WeixinSecCheckUtils.checkText(redisTemplate, weixinXcxConfig.getAppId(), weixinXcxConfig.getAppSecret(), ClientAppEnum.WEIXIN_XCX, text);
        }
        return true;
    }

    @Override
    public boolean checkImage(MultipartFile file) {
        String appid = WebUtils.getServletRequest().getHeader(AuthUtils.HEADER_PREFIX + AuthUtils.PARAM_APPID);
        if (StringUtils.isNotEmpty(appid) && appid.startsWith(AppTypeEnum.WEIXIN_XCX.getValue())) {
            WeixinXcxConfig weixinXcxConfig = sysConfigService.getByName(SysConfigEnum.WEIXIN_XCX_CONFIG.getValue(), WeixinXcxConfig.class);
            return WeixinSecCheckUtils.checkImage(redisTemplate, weixinXcxConfig.getAppId(), weixinXcxConfig.getAppSecret(), ClientAppEnum.WEIXIN_XCX, file);
        }
        return true;
    }

    @Override
    public boolean checkImage(InputStream inputStream) {
        String appid = WebUtils.getServletRequest().getHeader(AuthUtils.HEADER_PREFIX + AuthUtils.PARAM_APPID);
        if (StringUtils.isNotEmpty(appid) && appid.startsWith(AppTypeEnum.WEIXIN_XCX.getValue())) {
            WeixinXcxConfig weixinXcxConfig = sysConfigService.getByName(SysConfigEnum.WEIXIN_XCX_CONFIG.getValue(), WeixinXcxConfig.class);
            return WeixinSecCheckUtils.checkImage(redisTemplate, weixinXcxConfig.getAppId(), weixinXcxConfig.getAppSecret(), ClientAppEnum.WEIXIN_XCX, inputStream);
        }
        return true;
    }

    @Autowired
    public void setSysConfigService(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

}
